package com.quasar.hpatient.presenter;

import android.view.View;

import com.quasar.hpatient.MainAppliaction;
import com.quasar.hpatient.constant.ConstantManger;
import com.quasar.hpatient.model.httpmodel.AnotherResult;
import com.quasar.hpatient.model.httpmodel.DataStateResult;
import com.quasar.hpatient.model.medicalmodel.CommunityData;
import com.quasar.hpatient.model.medicalmodel.PatientInfoData;
import com.quasar.hpatient.model.requestmodel.RequestModel;
import com.quasar.hpatient.onInterface.AnotherResultListener;
import com.quasar.hpatient.onInterface.CircleContract;
import com.quasar.hpatient.utils.TimeZoneUtil;
import com.quasar.hpatient.view.community.bean.CommentConfig;
import com.vise.log.ViseLog;

import java.util.Date;

/**
 * author : 许建宏
 * Date: 2017/12/6 0006
 * View:
 */

public class CirclePresenter implements CircleContract.Presenter {
    private CircleContract.View view;
    private RequestModel requestModel;
    PatientInfoData patientInfoData= MainAppliaction.getInstance().getPatient();

    public CirclePresenter(CircleContract.View view){
        requestModel = new RequestModel();
        this.view = view;
    }

    @Override
    public void loadData(final int loadType) {

        requestModel.GetPostListData(loadType + "", new AnotherResultListener<AnotherResult<CommunityData>>() {
            @Override
            public void onSuccess(AnotherResult<CommunityData> data) {
                view.update2loadData(loadType, data);
            }

            @Override
            public void onError(String msg) {
                view.onError("加载圈子数据失败");
                ViseLog.d("加载圈子数据失败"+msg);
            }

            @Override
            public void onRefreshUI() {

            }
        });
    }

    @Override
    public void deleteCircle(String circleId) {

    }

    //点赞
    @Override
    public void addFavort(int circlePosition) {

    }

    //取消点赞
    @Override
    public void deleteFavort(int circlePosition, String favortId) {

    }

    @Override
    public void deleteComment(int circlePosition, String commentId) {

    }


    //新增评论
    public void addComment(final String content, final CommunityData.ListCommentBean config){

        if(config!=null){
            requestModel.UpdateCommentPostData(config.getPostId(), content, new AnotherResultListener<DataStateResult>() {
                @Override
                public void onSuccess(DataStateResult data) {

                    ViseLog.d("评论的消息"+data.getMsgCode());
                    if(data.getMsgCode().equals("0x00000000")){

                        CommunityData.ListCommentBean commentBean=new CommunityData.ListCommentBean();
                        commentBean.setUserId(Integer.parseInt(patientInfoData.getId()+"")) ;
                        commentBean.setCommentTime(TimeZoneUtil.sdf.format(new Date()).substring(0,10));
                        commentBean.setNick(patientInfoData.getNick());
                        commentBean.setComment(content);
                        commentBean.setUserType(Integer.parseInt(ConstantManger.userType));

                        if (view != null) {
                            view.update2AddComment(config.getNoteId(), commentBean);
                        }
                    }
                }

                @Override
                public void onError(String msg) {
                    ViseLog.d("评论失败"+msg);
                    view.onError("评论失败");
                }

                @Override
                public void onRefreshUI() {

                }
            });
        }

    }

    /**
     *
     * @param commentConfig
     */
    public void showEditTextBody(CommunityData.ListCommentBean commentConfig){

        ViseLog.d("评论的消息"+commentConfig.getComment());


        if(view != null){
            view.updateEditTextBodyVisible(View.VISIBLE, commentConfig);
        }
    }

    /**
     * 清除对外部对象的引用，反正内存泄露。
     */
    public void recycle(){
        this.view = null;
    }
}
